package com.example.petgrowing;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity3 extends AppCompatActivity {
    public String TAG = "shoping-petgrowing";
    String[] items = {"Product A(Satisfy value+10)", "Product B(Satisfy value+20)", "Product C(Satisfy value+30)", "Product D(Satisfy value+40)", "Product E(Satisfy value+50)"};
    double[] prices = {5.0, 10.0, 15.0, 20.0, 25.0};
    int[] quantities = new int[items.length];
    public LinearLayout cartPanel;
    public TextView totalPriceView;
    public double walletBalance;
    public double totalPrice;
    public double buysatisfyvalue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping_activity);

        walletBalance = getIntent().getDoubleExtra("Money", 0);
        updateWalletBalance(walletBalance);

        LinearLayout itemPanel = findViewById(R.id.item_panel);
        cartPanel = findViewById(R.id.cart_panel);
        totalPriceView = findViewById(R.id.total_price_view);
        TextView walletBalanceView = findViewById(R.id.wallet_balance);
        Button checkoutBtn = findViewById(R.id.checkout_btn);
        Button GoBackHome = findViewById(R.id.gobackhome_button);

        for (int i = 0; i < items.length; i++) {
            addRow(itemPanel, i);
        }

        walletBalanceView.setText(String.format("¥ %.2f", walletBalance));

        checkoutBtn.setOnClickListener(view -> {
            if (totalPrice > walletBalance) {
                Toast.makeText(this, "余额不足，请重新选择商品", Toast.LENGTH_SHORT).show();
            } else {
                walletBalance -= totalPrice;
                updateWalletBalance(walletBalance);
                updateCart();
                buysatisfyvalue = totalPrice;
                Intent data = new Intent();
                data.putExtra("Money",walletBalance);
                Log.i(TAG, String.valueOf(totalPrice));
                data.putExtra("GrowSatisfy",buysatisfyvalue);
                setResult(RESULT_OK,data);

                Log.i(TAG, String.valueOf(walletBalance)+"购买完成");
                Toast.makeText(this, "购买完成，感谢您的惠顾！", Toast.LENGTH_SHORT).show();

                cartPanel.removeAllViews();
                for (int i = 0; i < quantities.length; i++) {
                    quantities[i] = 0;
                }
                totalPriceView.setText("0.00");
            }
        });

        GoBackHome.setOnClickListener(view -> {
            updateWalletBalance(walletBalance);
            Log.i(TAG, String.valueOf(walletBalance)+"回家");
            updateCart();
            Intent data = new Intent();
            data.putExtra("Money",walletBalance);
            data.putExtra("GrowSatisfy",buysatisfyvalue);
            setResult(RESULT_OK,data);
            finish();
        });
    }

    private void addRow(LinearLayout panel, int index) {
        View rowView = getLayoutInflater().inflate(R.layout.item_row, panel, false);

        TextView nameTV = rowView.findViewById(R.id.item_name);
        Button addButton = rowView.findViewById(R.id.add_button);
        TextView priceTV = rowView.findViewById(R.id.item_price);

        nameTV.setText(items[index]);
        priceTV.setText(String.format("¥ %.2f", prices[index]));

        addButton.setOnClickListener(view -> {
            quantities[index]++;
            updateCart();
        });

        panel.addView(rowView);
    }

    private void updateCart() {
        cartPanel.removeAllViews();
        totalPrice = 0;

        for (int i = 0; i < items.length; i++) {
            if (quantities[i] > 0) {
                View itemView = getLayoutInflater().inflate(R.layout.cart_item, cartPanel, false);

                TextView nameTV = itemView.findViewById(R.id.item_name);
                TextView quantityTV = itemView.findViewById(R.id.quantity);
                TextView priceTV = itemView.findViewById(R.id.item_price);

                nameTV.setText(items[i]);
                quantityTV.setText(String.valueOf(quantities[i]));
                double itemPrice = quantities[i] * prices[i];
                priceTV.setText(String.format("¥ %.2f", itemPrice));

                cartPanel.addView(itemView);
                totalPrice += itemPrice;
            }
        }

        totalPriceView.setText(String.format("¥ %.2f", totalPrice));
    }

    private void updateWalletBalance(double balance) {
        TextView walletBalanceView = findViewById(R.id.wallet_balance);
        walletBalanceView.setText(String.format("¥ %.2f", balance));
    }
}